package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import DAO.LernbgrpDAO;
import Model.Lernbuero;

public class LernbueroDAO {
	
	private LernbgrpDAO lernbgrpDAO=new LernbgrpDAO();
	
	public Lernbuero getLernbuero(int id)
	{
		Lernbuero lernbuero=null;
		Connection conn=ConnectionProvider.getConnection();
		PreparedStatement statement=null;
		ResultSet rs=null;
		String sql="select * from lernbuero where lbID=?";
        try {
			statement = conn.prepareStatement(sql);
			statement.setInt(1, id);
			rs=statement.executeQuery();
			
			if (rs.next())
			{
				lernbuero=new Lernbuero();
				lernbuero.setId(id);
				lernbuero.setLernbbez(rs.getString("lernbbez"));
				lernbuero.setLernbgrp(lernbgrpDAO.getLernbgrp(rs.getInt("lbgruppeID")));
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		finally {
			try {
				rs.close();
				statement.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return lernbuero;
	}
	
	public void update(Lernbuero lernbuero)
	{
		Connection conn=ConnectionProvider.getConnection();
		String sql="update lernbuero set lernbbez=?, lbgruppeID=? where lbID=?";
        try {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setString(1, lernbuero.getLernbbez());
			statement.setInt(2, lernbuero.getLernbgrp().getId());
			statement.setInt(6, lernbuero.getId());

			statement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void anlegen(Lernbuero lernbuero)
	{
		Connection conn=ConnectionProvider.getConnection();
		String sql="insert into lernbuero VALUES (?, ?, ?)";
        try {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setInt(1, lernbuero.getId());
			statement.setString(2, lernbuero.getLernbbez());
			statement.setInt(3, lernbuero.getLernbgrp().getId());
			

			statement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void delete(int id)
	{
		Connection conn=ConnectionProvider.getConnection();
		String sql="delete from lernbuero where lbID= ?";
        try {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setInt(1, id);
			statement.executeUpdate();
        } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
        finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
